import router from './router'
import Store from '@/store'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
const whiteList = ['/login', '/404']
// 前置守卫
router.beforeEach(async(to, from, next) => {
  Nprogress.start()
  if (Store.getters.token) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      if (Store.state.user.userInfo.userId) {
        next()
      } else {
        await Store.dispatch('user/getUserInfo')
        // 新添加的动态路由需要重新走一遍导航守卫
        next(to.fullPath)
      }
    }
  } else {
    if (whiteList.includes(to.path.toLowerCase())) {
      next()
    } else {
      // fullPath 是携带参数的路由地址
      // path 是不携带参数的地址
      next('/login?add=' + to.fullPath)
    }
  }
  Nprogress.done()
})
router.afterEach(() => {
  Nprogress.done()
})
